package archer.framework.utils;

import jodd.servlet.ServletUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Servlet工具类
 *
 * @author christ
 * @date 2015/11/2
 */
public class ServletUtils extends ServletUtil {


    /**
     * 获得当前请求线程对应的HttpServletRequest
     *
     * @return
     */
    public static HttpServletRequest getCurrentRequest() {

        return RequestContextHolder.getRequestAttributes() == null
                ? null
                : ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     * 获得当前请求线程对应的HttpServletResponse
     *
     * @return
     */
    public static HttpServletResponse getCurrentResponse() {

        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    }

    /**
     * 获得request
     *
     * @param request
     * @return
     */
    public static HttpServletRequest getRequest(ServletRequest request) {

        return (HttpServletRequest) request;
    }

    /**
     * 获得response
     *
     * @param response
     * @return
     */
    public static HttpServletResponse getResponse(ServletResponse response) {

        return (HttpServletResponse) response;
    }

    /**
     * 获得服务端ip地址
     *
     * @param request
     * @return
     */
    public static String getLocalAddr(ServletRequest request) {

        return request.getLocalAddr();
    }

    /**
     * 获得请求路径
     *
     * @param request
     * @return
     */
    public static String getRequestURI(ServletRequest request) {

        return getRequest(request).getRequestURI();
    }

    /**
     * 获得method
     *
     * @param request
     * @return
     */
    public static String getMethod(ServletRequest request) {

        return getRequest(request).getMethod();
    }

    /**
     * 获得cookie
     *
     * @param request
     * @return
     */
    public static Cookie[] getCookies(ServletRequest request) {

        return getRequest(request).getCookies();
    }

    /**
     * 获得查询参数
     *
     * @param request
     * @return
     */
    public static String getQueryString(ServletRequest request) {

        return getRequest(request).getQueryString();
    }

    /**
     * 设置头信息
     *
     * @param response
     * @param name
     * @param value
     */
    public static void setHeader(ServletResponse response, String name, String value) {

        getResponse(response).setHeader(name, value);
    }

    /**
     * 获得头部信息
     *
     * @param request
     * @param name
     * @return
     */
    public static String getHeader(ServletRequest request, String name) {

        return getRequest(request).getHeader(name);
    }

    /**
     * 获取客户端ip
     *
     * @param request
     * @return
     */
    public static String getClientIp(ServletRequest request) {

        String ip = getHeader(request, "X-Real-IP");

        if (ValidateUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {

            ip = getHeader(request, "Proxy-Client-IP");
        }

        if (ValidateUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {

            ip = getHeader(request, "WL-Proxy-Client-IP");
        }

        if (ValidateUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {

            return getRequest(request).getRemoteAddr();
        }

        return ip.split(",")[0];
    }


    /**
     * redirect
     *
     * @param request
     * @param response
     * @param url
     */
    public static void redirect(ServletRequest request, ServletResponse response, String url) {

        try {

            getResponse(response).sendRedirect(getRequest(request).getContextPath() + url);

        } catch (IOException e) {

            e.printStackTrace();
        }

    }
}
